package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbStudentHave;
import com.baike.service.impl.TbStudentHaveServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lbs
 * @since 2021-10-30
 */
@RestController
@RequestMapping("studentHave")
@Api("学生购买课程管理中心")
public class TbStudentHaveController {

    @Autowired
    private TbStudentHaveServiceImpl studentHaveService;

    @PostMapping
    @ApiOperation("学生购买课程记录增加")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "学生ID",name = "sId",required = true,paramType = "query"),
            @ApiImplicitParam(value = "课程ID",name = "cId",required = true,paramType = "query")
    })
    public Result<ConstantEnum> postHave(@RequestParam("sId")Long sId ,@RequestParam("cId")Long cId){
        // 非空判断
        if(Objects.nonNull(sId) && Objects.nonNull(sId)){
            boolean b = studentHaveService.save(new TbStudentHave().setStudentId(sId).setCourseId(cId));
            if (b){
                return Result.build(ConstantEnum.SUCCESS);
            }else {
                return Result.build(ConstantEnum.ERROR);
            }
        }
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

    @DeleteMapping
    @ApiOperation("学生购买课程记录删除")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "学生id",name = "sId",required = true,paramType = "query"),
            @ApiImplicitParam(value = "课程id 数组",name = "cIds",required = true,paramType = "query")
    })
    public Result<ConstantEnum> delHave(@RequestParam("sId")Long sId , @RequestParam("cIds")Long[] cIds){
        // 非空判断
        if(Objects.nonNull(sId) && Objects.nonNull(cIds)){
            LambdaQueryWrapper<TbStudentHave> wrapper = Wrappers.<TbStudentHave>lambdaQuery()
                    .eq(TbStudentHave::getStudentId, sId)
                    .in(TbStudentHave::getCourseId, Arrays.asList(cIds));
            boolean b = studentHaveService.remove(wrapper);
            if (b){
                return Result.build(ConstantEnum.SUCCESS);
            }else {
                return Result.build(ConstantEnum.ERROR);
            }
        }
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

    @PostMapping("findHaveBySid")
    @ApiOperation("根据学生id 查询购买记录")
    @ApiImplicitParam(value = "学生id",name = "sId",required = true,paramType = "query")
    public Result findBySid(@RequestParam("sId")Long sId){
        // 非空判断
        if(Objects.nonNull(sId)){
            LambdaQueryWrapper<TbStudentHave> wrapper = Wrappers.<TbStudentHave>lambdaQuery().eq(TbStudentHave::getStudentId,sId);
            List<TbStudentHave> list = studentHaveService.list(wrapper);
            if(list.size() > 0){
                return Result.build(ConstantEnum.SUCCESS).setResult(list);
            }
            return Result.build(ConstantEnum.NULL_DATA);

        }
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }


}
